package com.zjy.service.impl;

import com.zjy.bean.Goods;
import com.zjy.bean.ShoppingCart;
import com.zjy.dao.GoodsMapper;
import com.zjy.dao.ShoppingCartMapper;
import com.zjy.service.ShoppingCartService;
import com.zjy.utils.ObjectUtils;
import com.zjy.utils.UUIDUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2019/5/19.
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addShopCart(ShoppingCart shoppingCart) {
        String goodsId = shoppingCart.getGoodsId();
        String userId = shoppingCart.getUserId();
        //查询此用户、此商品是否已存在购物车
        ShoppingCart sc = shoppingCartMapper.selectByUserAndGoodsId(userId,goodsId);
        //查询商品，获取商品销售价和折扣价
        Goods goods = goodsMapper.selectGoodsById(goodsId);
        BigDecimal price = null;
        BigDecimal discountPrice = null;
        BigDecimal amountOfMoney = null;
        String goodsName = null;
        String goodsCover = null;
        if(sc == null){         //没有找到对应的购物车数据，新增一条数据
            if(goods != null){
                price = goods.getPrice();
                discountPrice = goods.getDiscountPrice();
                goodsName = goods.getChineseName();
                goodsCover = goods.getGoodsCover();
            }
            //第一次新增，quantity为1，小计就等于折扣价或销售价
            if(discountPrice != null){  //有折扣价则用折扣价当做小计
                amountOfMoney = discountPrice;
            }else if(price != null){  //没有折扣价则用销售价当做小计
                amountOfMoney = price;
            }else {
                amountOfMoney = BigDecimal.ZERO;
            }
            shoppingCart.setId(UUIDUtils.getUUID());//设置商品名称、商品封面图、销售价、折扣价、数量、小计
            shoppingCart.setGoodsName(goodsName);
            shoppingCart.setGoodsCover(goodsCover);
            shoppingCart.setPrice(price);
            shoppingCart.setDiscountPrice(discountPrice);
            shoppingCart.setQuantity(1);
            shoppingCart.setAmountOfMoney(amountOfMoney);
            shoppingCartMapper.insertSelective(shoppingCart);
        }else {     //找到购物车记录，直接将数量加1，计算小计
            price = sc.getPrice();
            discountPrice = sc.getDiscountPrice();
            amountOfMoney = sc.getAmountOfMoney();
            if(discountPrice != null){  //有折扣价，小计=小计+折扣价
                amountOfMoney = amountOfMoney.add(discountPrice);
            }else if(price != null){   //没有折扣价，小计=小计+销售价
                amountOfMoney = amountOfMoney.add(price);
            }
            //更新小计、数量+1
            sc.setQuantity(sc.getQuantity() + 1);
            sc.setAmountOfMoney(amountOfMoney);
            shoppingCartMapper.updateByPrimaryKeySelective(sc);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int subtractShopCartById(ShoppingCart shoppingCart) {
        int count = 0;
        //查询此用户、此商品是否已存在购物车
        ShoppingCart sc = shoppingCartMapper.selectByUserAndGoodsId(shoppingCart.getUserId(),shoppingCart.getGoodsId());
        if(sc != null){
            BigDecimal price = sc.getPrice();
            BigDecimal discountPrice = sc.getDiscountPrice();
            BigDecimal amountOfMoney = sc.getAmountOfMoney();
            if(discountPrice != null){  //有折扣价，小计=小计-折扣价
                amountOfMoney = amountOfMoney.subtract(discountPrice);
            }else if(price != null){   //没有折扣价，小计=小计-销售价
                amountOfMoney = amountOfMoney.subtract(price);
            }
            //更新小计、数量-1
            sc.setQuantity(sc.getQuantity() - 1);
            sc.setAmountOfMoney(amountOfMoney);
            count = shoppingCartMapper.updateByPrimaryKeySelective(sc);
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteShopCartById(List<String> idList) {
        return shoppingCartMapper.deleteShopCartById(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ShoppingCart> selectShopCartByUserId(String userId) {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShopCartByUserId(userId);
        if(!ObjectUtils.isNullOrEmpty(shoppingCartList)){
            for (ShoppingCart shoppingCart : shoppingCartList) {
                String goodsId = shoppingCart.getGoodsId();
                Goods goods = goodsMapper.selectGoodsById(goodsId);
                if(goods != null){
                    shoppingCart.setStockNum(goods.getStockNum());
                }
            }
        }
        return shoppingCartList;
    }
}
